var express = require('express');
var router = express.Router();
//引入连接池
var pool = require('../pool.js');

//第三方图形验证码模块:
//下载  npm i svg-captcha
var svgCaptcha = require('svg-captcha');

// 主页面接口index 
router.get('/v1/index', (req,res) => {
	var sql = "select * from sd_index";
	console.log(sql);
	pool.query(sql,(err,result) => {
		if (err) throw err;
		if (result.length > 1) {
			res.send({
				code: 200, msg: "查询成功",data:result
			})
		} else {
			res.send({ code: 400, msg: "查询失败" })
		}	
	})
})

// 新闻资讯接口consult
router.get('/v1/consult', (req,res) => {
	var sql = "select * from sd_consult";
	console.log(sql);
	pool.query(sql,(err,result) => {
		if (err) throw err;
		if (result.length > 1) {
			res.send({
				code: 200, msg: "查询成功",data:result
			})
		} else {
			res.send({ code: 400, msg: "查询失败" })
		}	
	})
})

// 新闻详情接口sd_article
router.get('/v1/article/:aid', (req,res) => {
	var _aid=req.params.aid;
	console.log(_aid);
	var sql = "select * from sd_article where aid=?";
	pool.query(sql,[_aid],(err,result) => {
		if (err) throw err;
		if(result.length>0){
			res.send({
				msg:"成功！",
				data:result
			})
		}else{
			res.send({msg:"失败！"})
		}
	})
})

// 新闻动态接口sd_trends
router.get('/v1/trends', (req,res) => {
	var sql = "select * from sd_trends";
	console.log(sql);
	pool.query(sql,(err,result) => {
		if (err) throw err;
		if (result.length > 1) {
			res.send({
				code: 200, msg: "查询成功",data:result
			})
		} else {
			res.send({ code: 400, msg: "查询失败" })
		}	
	})
})

// 动态详情接口sd_dynamic
router.get('/v1/dynamic/:did', (req,res) => {
	var _did=req.params.did;
	console.log(_did);
	var sql = "select * from sd_dynamic where did=?";
	pool.query(sql,[_did],(err,result) => {
		if (err) throw err;
		if(result.length>0){
			res.send({
				msg:"成功！",
				data:result
			})
		}else{
			res.send({msg:"失败！"})
		}
	})
})

// 粥品商品接口sd_porridge
router.get('/v1/porridge', (req,res) => {
	var sql = "select * from sd_trends";
	pool.query(sql,(err,result) => {
		if (err) throw err;
		if (result.length > 1) {
			res.send({
				code: 200, msg: "查询成功",data:result
			})
		} else {
			res.send({ code: 400, msg: "查询失败" })
		}	
	})
})
// 粥品详情接口sd_porridge_detail
router.get('/v1/porridge_detail/:pd_id', (req,res) => {
	var pd_id=req.params.pd_id;
	console.log(pd_id);
	var sql = "select * from sd_porridge_detail where pd_id=?";
	pool.query(sql,[pd_id],(err,result) => {
		if (err) throw err;
		if(result.length>0){
			res.send({
				msg:"成功！",
				data:result
			})
		}else{
			res.send({msg:"失败！"})
		}
	})
})

// 小菜商品接口sd_sidedish
router.get('/v1/sidedish', (req,res) => {
	var sql = "select * from sd_sidedish";
	pool.query(sql,(err,result) => {
		if (err) throw err;
		if (result.length > 1) {
			res.send({
				code: 200, msg: "查询成功",data:result
			})
		} else {
			res.send({ code: 400, msg: "查询失败" })
		}	
	})
})

// 小菜详情接口sd_sidedish_detail
router.get('/v1/sidedish_detail/:sd_id', (req,res) => {
	var sd_id=req.params.sd_id;
	console.log(sd_id);
	var sql = "select * from sd_sidedish_detail where sd_id=?";
	pool.query(sql,[sd_id],(err,result) => {
		if (err) throw err;
		if(result.length>0){
			res.send({
				msg:"成功！",
				data:result
			})
		}else{
			res.send({msg:"失败！"})
		}
	})
})

// 点心商品接口sd_dessert
router.get('/v1/dessert', (req,res) => {
	var sql = "select * from sd_dessert";
	pool.query(sql,(err,result) => {
		if (err) throw err;
		if (result.length > 1) {
			res.send({
				code: 200, msg: "查询成功",data:result
			})
		} else {
			res.send({ code: 400, msg: "查询失败" })
		}	
	})
})
// 点心详情接口sd_dessert_detail
router.get('/v1/dessert_detail/:dd_id', (req,res) => {
	var dd_id=req.params.dd_id;
	console.log(dd_id);
	var sql = "select * from sd_dessert_detail where dd_id=?";
	pool.query(sql,[dd_id],(err,result) => {
		if (err) throw err;
		if(result.length>0){
			res.send({
				msg:"成功！",
				data:result
			})
		}else{
			res.send({msg:"失败！"})
		}
	})
})

// 蒸品商品接口sd_steam
router.get('/v1/steam', (req,res) => {
	var sql = "select * from sd_steam";
	pool.query(sql,(err,result) => {
		if (err) throw err;
		if (result.length > 1) {
			res.send({
				code: 200, msg: "查询成功",data:result
			})
		} else {
			res.send({ code: 400, msg: "查询失败" })
		}	
	})
})
// 蒸品详情接口sd_steam_detail
router.get('/v1/steam_detail/:sd_id', (req,res) => {
	var sd_id=req.params.sd_id;
	console.log(sd_id);
	var sql = "select * from sd_steam_detail where sd_id=?";
	pool.query(sql,[sd_id],(err,result) => {
		if (err) throw err;
		if(result.length>0){
			res.send({
				msg:"成功！",
				data:result
			})
		}else{
			res.send({msg:"失败！"})
		}
	})
})

module.exports = router;
